# -*- coding:utf-8 -*-

from my_http import MyHttp
import urllib.request
from bs4 import BeautifulSoup
import glob
import jieba
import re
import threading
import hashlib
import urllib.parse
import time
import timedelta
import datetime
import requests
from my_time import MyTime
from common.attr_v3 import AttrV3
from database.db_handler import MysqlHander
from database.db_business import DbBusiness
import json
import os
from selenium import webdriver

'''
上交所监管数据
'''

file_relation = open('./out/shenjiaosuo_zhishu_relation_v1.txt', 'a+', encoding = 'utf-8')
class SzZhishuRelationData:
    def __init__(self, file):
        self.file = file
        self.db = MysqlHander("./config.ini")
        pass

    def query_page_data(self, page, code):
        time.sleep(1)
        url = "http://www.cnindex.com.cn/sample-detail/detail?indexcode=" + str(code) + "&dateStr=2022-01&pageNum=" + str(page) + "&rows=20"
        json_data = json.loads(MyHttp.http_json_data(url))
        total = int(json_data["total"])

        if 0 == total:
            return False

        for d in json_data["data"]["rows"]:
            file_relation.write(d["seccode"] + "|0|2001|" + self.attr + "|0|" + "\n")

        if total <= (page * 20):
            return False
        return True
        
    def query_data(self, data):
        #try:
        data_split = data.split("|")
        self.attr = data_split[2]
        url_split = data_split[3].split("=")
        data_code = url_split[2]
        page = 1
        while(self.query_page_data(page, data_code)):
            page = page + 1
        #except Exception as e:
        #    print(str(e))
        #    pass
        
    
    def get_data(self):
        self.stock_list = DbBusiness.query_stock_code(self.db)
        with open(self.file, 'r', encoding='utf-8') as f:
            self.business = f.readlines()
        i = 1
        for b in self.business:
            print(i)
            if i <= 731:
                i = i + 1
            else:
                self.query_data(b)
                i = i + 1

if __name__ == '__main__':
    d = SzZhishuRelationData("./out/shenjiaosuo_zhishu_list_data.txt")
    d.get_data()
    file_relation.close()
